<template>
  <div class="uint_management">
    <el-form :inline="true" :model="elform" class="demo-form-inline">
      <el-form-item label="名称:">
        <el-input v-model="elform.unitName" placeholder="名称" size="mini" clearable></el-input>
      </el-form-item>
      <el-form-item label="编号:">
        <el-input v-model="elform.unitCode" placeholder="编号" size="mini" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" size="mini" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item class="addsubmit">
        <el-button type="primary" @click="addsubmit" size="mini" icon="el-icon-plus" v-if="isauth('unit_add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="tableData"
      border
      highlight-current-row
      style="width: 100%"
      :header-cell-style="headFirst"
      :cell-style="(res) => headFirst_col(res, [])"
    >
      <el-table-column prop="unitName" label="名称"></el-table-column>
      <el-table-column prop="unitCode" label="编号"></el-table-column>
      <el-table-column prop="unitAddress" label="地址"></el-table-column>
      <el-table-column prop="linkMan" label="联系人"></el-table-column>
      <el-table-column prop="phoneNumber" label="联系电话"></el-table-column>
      <el-table-column prop="fax" label="传真"></el-table-column>
      <el-table-column prop="website" label="网址"></el-table-column>
      <el-table-column prop="unitGuid" label="单位序列号"></el-table-column>
      <el-table-column prop="unitType" label="类型" v-if="showUnitType"></el-table-column>
      <el-table-column width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="userType === '1'  && isauth('unit_setting') && (unitType == 50)"
            type="text"
            size="mini"
            @click.stop="parameterSetting(scope.row)"
          >配置参数</el-button>
          <el-button
            type="text"
            size="mini"
            @click.stop="addOrUpdateHandle(scope.row)"
            v-if="isauth('unit_update')"
          >修改</el-button>
          <el-button
            type="text"
            v-if="scope.row.unitType==100 && userType === '1'"
            size="mini"
            style="color:red"
            @click.stop="clearAllData(scope.row)"
          >清空数据</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChangepage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="recordCount"
      ></el-pagination>
    </div>
    <!-- 新增弹窗 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="initpage"
      :userGuid="userGuid"
    ></add-or-update>
    <!-- 配置参数 -->
    <parameter-setting v-if="showParamenterSetting" ref="parameterSetting" :userGuid="userGuid"></parameter-setting>
  </div>
</template>

<script>
import AddOrUpdate from "./unit-add-or-update";
import ParameterSetting from "./unit-parameter";
export default {
  data() {
    return {
      showUnitType: false,
      elform: {
        unitCode: "",
        unitName: ""
      },
      searchInfo: {},
      tableData: [],
      dataListLoading: false,
      pageSize: 10,
      pageNum: 1,
      currentPage: 1,
      recordCount: 0,
      userGuid: "",
      unitType:'',
      addOrUpdateVisible: false,
      showParamenterSetting: false,
      showPayConfig: false
    };
  },
  components: {
    AddOrUpdate,
    ParameterSetting
  },
  created() {
    this.userGuid = sessionStorage.getItem("administrator");
    this.userType = sessionStorage.getItem("userType");
    this.unitType = sessionStorage.getItem("unitType");
    this.superiorGuid = this.$store.state.user.changeschoolid;
  },

  watch: {
    // 'elform.unitCode': function (curVal, oldVal) {
    //   if (!curVal) {
    //       this.elform.unitCode = ''
    //       return false
    //   }
    //   this.elform.unitCode = curVal.match(/\d/ig) ? curVal.match(/\d/ig).join('') : ''
    // },
  },

  mounted() {
    this.initpage();
  },

  methods: {
    //限制输入
    //单位列表数据
    initpage() {
      if (this.elform.unitCode != "" && this.elform.unitCode != null) {
        this.searchInfo.unitCode = this.elform.unitCode;
      }
      if (this.elform.unitName != "" && this.elform.unitName != null) {
        this.searchInfo.unitName = this.elform.unitName;
      }
      this.searchInfo.superiorGuid = this.superiorGuid;
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/unit/sub-unit-page"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          console.log(data);
          this.tableData = data.messageContent.data;
          this.recordCount = data.messageContent.recordCount;
          this.currentPage = data.messageContent.pageNum;
          this.dataListLoading = false;
          this.searchInfo = {};
        } else {
          this.$message.error(data.errorMessage);
          this.searchInfo = {};
        }
      });
    },
    onSubmit() {
      this.pageNum = 1;
      this.initpage();
    },
    //新增单位
    addsubmit() {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.addinit();
      });
    },
    addOrUpdateHandle(row) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.eidtinit(row);
      });
    },
    
    parameterSetting(row) {
      this.showParamenterSetting = true;
      this.$nextTick(() => {
        this.$refs.parameterSetting.unitinit(row)
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.initpage();
    },
    handleCurrentChangepage(val) {
      this.pageNum = val;
      this.initpage();
    },
    clearAllData(row) {
      this.$confirm(
        "是否清空学校[" + row.unitName + "]内除管理账户、单位配置外的所有数据?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          this.$confirm(
            "当前操作清空的数据无法还原，请谨慎操作，是否继续?",
            "警告",
            {
              cancelButtonText: "取消",
              confirmButtonText: "确定",
              type: "error"
            }
          )
            .then(() => {
              this.$http({
                url: this.$http.adornUrl("/reset/unit-data-all-clear"),
                method: "post",
                data: this.$http.adornData({
                  messageContent: {
                    unitGuid: row.unitGuid,
                    userGuid: this.userGuid
                  },
                  messageType: this.messageType
                })
              }).then(({ data }) => {
                if (data.messageCode == 200) {
                  this.$message({
                    type: "success",
                    message: "清空数据成功！"
                  });
                } else {
                  this.$message({
                    type: "error",
                    message: "清空数据失败！"
                  });
                  console.log(data.errorMessage);
                }
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消"
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.uint_management {
  padding: 10px;
  .el-table th {
    display: table-cell !important;
  }
  .demo-form-inline {
    height: 40px;
    display: flex;
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 0px;
    }
  }
  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>